package com.example.sicnuaccountingapp.utils

import com.example.sicnuaccountingapp.data.database.AppDatabase
import com.example.sicnuaccountingapp.data.entity.Category
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject
import com.example.sicnuaccountingapp.data.entity.TransactionType

class DatabaseInitializer @Inject constructor(
    private val database: AppDatabase
) {
    suspend fun initializeDefaultCategories(userId: Long) = withContext(Dispatchers.IO) {
        // 检查用户是否已有分类
        val existingCategories = database.categoryDao().getCategoriesByUserId(userId)
        if (existingCategories.isEmpty()) {
            // 支出分类
            val expenseCategories = listOf(
                Category(userId = userId, name = "餐饮", type = TransactionType.EXPENSE),
                Category(userId = userId, name = "交通", type = TransactionType.EXPENSE),
                Category(userId = userId, name = "购物", type = TransactionType.EXPENSE),
                Category(userId = userId, name = "娱乐", type = TransactionType.EXPENSE),
                Category(userId = userId, name = "医疗", type = TransactionType.EXPENSE),
                Category(userId = userId, name = "住房", type = TransactionType.EXPENSE),
                Category(userId = userId, name = "教育", type = TransactionType.EXPENSE),
                Category(userId = userId, name = "其他支出", type = TransactionType.EXPENSE)
            )

            // 收入分类
            val incomeCategories = listOf(
                Category(userId = userId, name = "工资", type = TransactionType.INCOME),
                Category(userId = userId, name = "奖金", type = TransactionType.INCOME),
                Category(userId = userId, name = "投资", type = TransactionType.INCOME),
                Category(userId = userId, name = "其他收入", type = TransactionType.INCOME)
            )

            // 插入默认分类
            database.categoryDao().insertCategories(expenseCategories + incomeCategories)
        }
    }
} 